package com.chonglie;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

public class ServLifecycleActivity extends Activity {

	private ServiceConnection serviceConnection = new ServiceConnection() {

		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}

		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.serv_lifecycle);
		Log.e("Activity", "onCreate");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.e("Activity", "onStart");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.e("Activity", "onResume");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.e("Activity", "onPause");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.e("Activity", "onStop");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.e("Activity", "onRestart");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.e("Activity", "onDestroy");
	}

	public void startService(View view) {
		Intent intent = new Intent(this, ServLifecycleService.class);
		Log.e("Activity", "startService");
		startService(intent);
		printState();
	}

	public void stopService(View view) {
		Intent intent = new Intent(this, ServLifecycleService.class);
		Log.e("Activity", "stopService");

		stopService(intent);

		printState();
	}

	public void bindService(View view) {
		Intent intent = new Intent(this, ServLifecycleService.class);
		Log.e("Activity", "bindService");
		bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);

		printState();
	}

	public void unbindService(View view) {
		Log.e("Activity", "unbindService");
		if ("onBind".equals(ServLifecycleService.state)) {
			unbindService(serviceConnection);
		}

		printState();
	}

	private void printState() {
		Log.e("Activity", ServLifecycleService.getState() == null ? ""
				: ServLifecycleService.getState());
	}

}
